package org.vectrics.recruiter.struts.tag.cv;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.util.DateUtil;
import org.vectrics.common.view.FieldTable;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.VectricsTagSupport;
import org.vectrics.user.Person;

/**
 * Clase que implementa el TAG responsable de mostrar el "Personal Data" en Resumes
 * @author maguero
 */
public class PersonViewTag extends VectricsTagSupport {

	private static final long serialVersionUID = -6595133912447441255L;
	static private transient Logger log = Logger.getLogger(StudiesViewTag.class);
	private FieldTable fieldTable = null;
	private String personId = "";
	public static String TABLE_FONT = "subFont";
	
	public String getPersonId() {
		return personId;
	}

	public void setPersonId(String personId) {
		this.personId = personId;
	}
	
	@Override
	protected String makeStartTag() {
		log.debug("PersonViewTag.makeStartTag() called");
		HttpServletRequest request = (HttpServletRequest)super.pageContext.getRequest();
		String html = "";
		// Si el id es nulo muestro un mensaje de error
		if (this.getId() != null) {
			html = "<div class='stdFont'> " + this.getPhrase("error.null.id.person") + " </div>"; 
		} else {
			// Creo la tabla
			Table pageTable = new Table();
			
			try {
				Long id = new Long(this.getPersonId());
				// Busco la persona con el id
				Person person = CoreServiceLocator.getPersonService().findPersonById(id);
				pageTable = this.getUserInfoTable(request, person);
				html += pageTable.toString();
			} catch (Exception ex) {
				// Si se encuentra un error en el proceso se escribe por pantalla.
				html = "<div class='errorMsgFont'>  ";
				if (ex.getMessage() != null) {
					html += ex.getMessage() + " <br /> ";
				}
				html +=  ex.toString() + " <br /> ";
				html += "</div>";
				log.error(ex);
			}
		}
		// Retorno el html generado
		return html;
	}
	
	protected Table getUserInfoTable(HttpServletRequest request, Person user) {
    	log.debug("getUserInfoTable() - method to make information html table");
        Table mainTable = new Table();
        mainTable.setWidth(620);
        Row mainRow = mainTable.addRow();
        Cell mainCell = mainRow.addCell();

        fieldTable = new FieldTable();
        fieldTable.setCellPadding(2);
        fieldTable.setWidth("100%");
        fieldTable.setValueCellWidth("65%");
        fieldTable.setValueFontClass("stdFont");
        mainCell.addElement(fieldTable);
    	
        fieldTable.showField(this.getPhrase("prompt.person.name"), user.getLastName() + ", " + user.getFirstName());
        String home = (user.getContactInfo().getHomeAddress() != null) ? user.getContactInfo().getHomeAddress().getAddress1Complete() : "";
	    fieldTable.showField(this.getPhrase("prompt.person.homeAddress"), home);
	    String date = (user.getDateOfBirth() != null) ? DateUtil.getFechaFormateada(user.getDateOfBirth()) : "";
	    fieldTable.showField(this.getPhrase("prompt.person.birthday"), date);
	    String country = (user.getBirthCountry() != null) ? user.getBirthCountry().getName() : "";
	    fieldTable.showField(this.getPhrase("prompt.person.country.birthday"), country);
	    String gender = ((user.getGender() != null) && (user.getGender().equals("m"))) ? this.getPhrase("prompt.person.gender.male") : this.getPhrase("prompt.person.gender.female");
	    fieldTable.showField(this.getPhrase("prompt.person.gender"), gender);
	    fieldTable.showField(this.getPhrase("prompt.person.email"), user.getContactInfo().getEmail());
	    boolean nulo = ((user.getContactInfo() == null) || (user.getContactInfo().getHomePhone() == null)) ? true : false;
	    String phone = (!nulo) ? user.getContactInfo().getHomePhone().getFormattedNumber() : "";
	    fieldTable.showField(this.getPhrase("prompt.person.homePhone"), phone);
	    nulo = ((user.getContactInfo() == null) || (user.getContactInfo().getWorkPhone() == null)) ? true : false;
	    phone = (!nulo) ? user.getContactInfo().getWorkPhone().getFormattedNumber() : "";
	    fieldTable.showField(this.getPhrase("prompt.person.workPhone"), phone);
	    //Manuel Added.
	    fieldTable.showField("CUIL/CUIT", user.getCuilOrCuit());
	    String emergency = (StringUtils.isEmpty(user.getEmergencyContactName())?"":user.getEmergencyContactName());
	    fieldTable.showField("Emergency Contact Name", emergency);
	    emergency= StringUtils.EMPTY;
	    emergency = (StringUtils.isEmpty(user.getEmergencyContactPhoneNumer())?"":user.getEmergencyContactPhoneNumer());
	    fieldTable.showField("Emergency Contact Phone", emergency);
	    
        mainTable.setPrettyPrint(true);
        return(mainTable);
    }

}
